﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class Invoice2RptList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnExport;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlTable DbTotal;
        protected HtmlForm F15661;
        protected GridView gvInvoice;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnDueFromTo;
        protected HtmlInputHidden hdnFrom;
        protected Label lblInvoiceList;
        protected Label lblMsg;
        protected Label lblTotalAmount;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2InvoiceList(InvoiceRptQueryEntity invoiceRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(invoiceRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetInvoiceRpt2DataSet(invoiceRptQueryEntity);
            this.gvInvoice.Visible = true;
            if (ds.Tables.Count > 1)
            {
                this.CreateColumns(ds.Tables[2]);
            }
            else
            {
                this.CreateColumns(ds.Tables[0]);
            }
            base.BindData2GridView(this.gvInvoice, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.IsGetAll = true;
            string currentPageIndex = invoiceRptQueryEntity.CurrentPage;
            invoiceRptQueryEntity.CurrentPage = "0";
            this.gvInvoice.Columns[1].Visible = false;
            this.gvInvoice.AllowSorting = false;
            this.BindData2InvoiceList(invoiceRptQueryEntity);
            this.hdnActionID.Value = "";
            this.gvInvoice.ShowFooter = false;
            invoiceRptQueryEntity.IsGetAll = false;
            invoiceRptQueryEntity.CurrentPage = currentPageIndex;
            this.SetInvoiceQueryEntity(invoiceRptQueryEntity);
            this.hdnActionID.Value = "";
            base.Export2Excel("InvoiceRpt.xls", this.gvInvoice);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.CurrentPage = "0";
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            invoiceRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            if (int.Parse(invoiceRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                invoiceRptQueryEntity.CurrentPage = (int.Parse(invoiceRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            if (int.Parse(invoiceRptQueryEntity.CurrentPage) > 0)
            {
                invoiceRptQueryEntity.CurrentPage = (int.Parse(invoiceRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        private void CreateColumns(DataTable dt)
        {
            int i;
            if (this.gvInvoice.Columns.Count <= 2)
            {
                int width = 100;
                for (i = 2; i < (dt.Columns.Count - 1); i++)
                {
                    if (((dt.Columns[i].ColumnName.ToUpper() != "ROWSTAT") && (dt.Columns[i].ColumnName.ToUpper() != "信誉额度")) || ((dt.Columns[i].ColumnName.ToUpper() == "信誉额度") && (this.hdnDueFromTo.Value.IndexOf("DueFrom") > 0)))
                    {
                        BoundField bf = new BoundField {
                            HeaderText = dt.Columns[i].ColumnName,
                            DataField = dt.Columns[i].ToString(),
                            SortExpression = "[" + dt.Columns[i].ToString() + "]"
                        };
                        if (dt.Columns[i].DataType.ToString().ToUpper() == "System.Decimal".ToUpper())
                        {
                            bf.DataFormatString = base.GetFormatDigitString();
                        }
                        bf.ItemStyle.CssClass = "GridViewRowWidth_100";
                        width += 100;
                        this.gvInvoice.Columns.Add(bf);
                        this.gvInvoice.Width = width;
                    }
                }
            }
            this.DbTotal.Width = this.gvInvoice.Width.ToString();
            this.lblTotalAmount.Text = "";
            string totalAmount = string.Empty;
            for (i = 5; i < (dt.Columns.Count - 1); i++)
            {
                decimal amount = 0M;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    amount += decimal.Parse(base.GetPreferenceDigit(decimal.Parse(dt.Rows[j][dt.Columns[i].ToString()].ToString())));
                }
                if (amount > 0M)
                {
                    object CS00002 = totalAmount;
                    totalAmount = string.Concat(new object[] { CS00002, dt.Columns[i].ColumnName, ":", amount, " " });
                }
            }
            this.lblTotalAmount.Text = totalAmount;
        }

        private InvoiceRptQueryEntity GetInvoiceQueryEntity()
        {
            return (base.PageQueryEntitySession as InvoiceRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                invoiceRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
        }

        protected void gvInvoice_Init(object sender, EventArgs e)
        {
        }

        protected void gvInvoice_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                if (this.hdnActionID.Value != "EXPORT")
                {
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
            }
        }

        protected void gvInvoice_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InvoiceRptQueryEntity invoiceRptQueryEntity = this.GetInvoiceQueryEntity();
                invoiceRptQueryEntity.SortField = e.SortExpression;
                if (invoiceRptQueryEntity.SortDirection == "ASC")
                {
                    invoiceRptQueryEntity.SortDirection = "DESC";
                }
                else if (invoiceRptQueryEntity.SortDirection == "DESC")
                {
                    invoiceRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2InvoiceList(invoiceRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InvoiceRptQueryEntity invoiceRptQueryEntity = new InvoiceRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    invoiceRptQueryEntity.BILLING_OBJECT = this.PreviousPage.BillingObject;
                    invoiceRptQueryEntity.BILLING_OBJECT_ID = this.PreviousPage.BillingObjectID;
                    invoiceRptQueryEntity.DUE_FROM_TO = this.PreviousPage.DueFromTo;
                    invoiceRptQueryEntity.INVOICE_DATE = this.PreviousPage.EndDate;
                }
                else
                {
                    invoiceRptQueryEntity.BILLING_OBJECT = "";
                    invoiceRptQueryEntity.BILLING_OBJECT_ID = "";
                    invoiceRptQueryEntity.DUE_FROM_TO = "";
                    invoiceRptQueryEntity.INVOICE_DATE = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            this.hdnDueFromTo.Value = invoiceRptQueryEntity.DUE_FROM_TO;
            invoiceRptQueryEntity.IsGetAll = false;
            invoiceRptQueryEntity.CurrentPage = "0";
            invoiceRptQueryEntity.PageSize = this.gvInvoice.PageSize.ToString();
            invoiceRptQueryEntity.SortField = " I.BILLING_OBJECT_ID";
            invoiceRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvInvoice.PageSize;
            this.SetInvoiceQueryEntity(invoiceRptQueryEntity);
            this.BindData2InvoiceList(invoiceRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvInvoice);
                this.LoadData();
            }
        }

        private void SetInvoiceQueryEntity(InvoiceRptQueryEntity invoiceRptQueryEntity)
        {
            base.PageQueryEntitySession = invoiceRptQueryEntity;
        }

        public Invoice2RptQuery PreviousPage
        {
            get
            {
                return (Invoice2RptQuery) base.PreviousPage;
            }
        }
    }
}

